﻿/*____________________________________________________
|______________	register of functions _______________| 
|____________________________________________________|

- create_hero()		creates hero as the var "Hero"

- update_hero()		check collision an move

- BuildMap()		load and create the level


extern

- Setup()			create levels 

*/


package{												//The begin of an .as file
	import flash.display.MovieClip;						//import some libraries
	import flash.events.Event;
	import flash.events.KeyboardEvent;

	public class Script extends MovieClip{				// start the script
	
	private const gravity:int = 1;
	private const max_speed:int = 8;
	
	private const walkspeed:int = 4;
	private const jumpspeed:int = 25;
	
	private const start_x:int = 50;
	private const start_y:int = 50;
	
	private var y_speed:int;
	private var x_speed:int;
	
	private var left:Boolean;
	private var up:Boolean;
	private var right:Boolean;
	private var space:Boolean;
	private var can_jump:Boolean;
	
	
	//New mechanics
	private var enabled_double_jump:Boolean;
	private var can_double_jump:Boolean;
	
		
	private var level:Array = new Array();
	private var tiles:Array = new Array();
	
	private var Map_data:Data = new Data;				// create a version of the Data.as
	
	private var Hero:hero = new hero;
	
		public function Script(){						// the init (will only be runned once)
			BuildMap();
			create_hero();
			addEventListener(Event.ENTER_FRAME, main);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
			stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
		}
		
		private function main(event:Event){
			update_hero();
		}
		
		private function key_down(event:KeyboardEvent){
			if(event.keyCode == 37){
				left = true;
			}
			if(event.keyCode == 38){
				up = true;
				if (enabled_double_jump) can_double_jump= true;
			}
			if(event.keyCode == 39){
				right = true;
			}
			if(event.keyCode == 40){
				Hero.gotoAndStop(2);
			}
		}
		
		private function key_up(event:KeyboardEvent){
			if(event.keyCode == 37){
				left = false;
			}
			if(event.keyCode == 38){
				up = false;
			}
			if(event.keyCode == 39){
				right = false;
			}
			if(event.keyCode == 40){
				Hero.gotoAndStop(1);
			}
		}
								
			
		
/*
///    ///		/////////	///////////		///////////
///    ///		///			////    ///		///		///
///    ///		///			////    ///		///		///
//////////		/////////	//////////		///		///
//////////		/////////	////	///		///		///
///    ///		///			////	///		///		///
///    ///		///			////	///		///		///
///    ///		/////////	////	///		///////////
*/
		private function create_hero(){
			addChild(Hero);
			Hero.x = start_x;
			Hero.y = start_y;
		}
		
		private function update_hero(){
			y_speed += gravity;
			if(left){
				x_speed -= walkspeed;
			}
			if(right){
				x_speed += walkspeed;
			}
			if (can_double_jump) {
				y_speed -= jumpspeed;
				can_double_jump= false;
			}
			if(up && can_jump){
				y_speed -= jumpspeed;
				can_jump = false;
			}
			
			if(y_speed > max_speed){
				y_speed = max_speed;
			}
			
			
			Hero.y += y_speed;
			Hero.x += x_speed;
			for(var t:int; t < tiles.length; t++){
				while(tiles[t].hitTestPoint(Hero.x,Hero.y + 10,true)){
					Hero.y -= 1;
					if(y_speed >= 0){									
						can_jump = true;
					}
				}
				while(tiles[t].hitTestPoint(Hero.x,Hero.y - 10,true)){
					Hero.y += 1;
					y_speed = 0;
				}
				while(tiles[t].hitTestPoint(Hero.x + 5,Hero.y,true)){
					Hero.x -= 1;
				}
				while(tiles[t].hitTestPoint(Hero.x - 5,Hero.y,true)){
					Hero.x += 1;
				}
			}
			x_speed = 0;
		}
	
	
/*
|||||||||||		 |||||||||||	  ||||||||||	||||||||||
||||||||||||	||||||||||||	 ||||	 ||||	||||	|||
||||	||||| |||||		||||	 ||||||||||||	||||||||||
||||	  |||||||		||||	 ||||	 ||||	||||
||||	    |||			||||	||||	  ||||	||||
||||					||||	||||      ||||	||||
*/
				
		private function BuildMap(){
			Map_data.Setup();												// setup data from extern file
			
			level = Map_data.level1;										// get data from extern file
			
			for(var t = 0; t < level.length; t++){
				for(var u = 0; u < level[t].length; u++){
					if(level[t][u] != 0){									//if the data is not null
						var new_tile:platform_tile = new platform_tile;		//than build a tile
						
						addChild(new_tile);									//put it on the screen
						
						new_tile.gotoAndStop(1);
						new_tile.x = u * new_tile.width;
						new_tile.y = t * new_tile.height;
						
						tiles.push(new_tile);								//put it in an array
					}
				}
			}
		}
	}
}
